package deployr.core;

import java.io.*;
import java.util.zip.*;

public class Unzip {

	private final String root;

	public Unzip(String root) {
		this.root = root;
	}

	public void unzip(InputStream inputStream) {
		try {
			BufferedOutputStream dest = null;
			ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream));
			ZipEntry entry;
			while ((entry = zip.getNextEntry()) != null) {
				int count;
				byte data[] = new byte[2048];

				File file = new File(root, entry.getName());
				String dirname = file.getParent();
				File dir = new File(dirname);
				dir.mkdirs();

				FileOutputStream fos = new FileOutputStream(file);
				dest = new BufferedOutputStream(fos, 2048);
				while ((count = zip.read(data, 0, 2048)) != -1) {
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
			}
			zip.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
